import type { MockMethod } from "vite-plugin-mock";

function generateAccessToken() {
  // 使用随机字符生成一个模拟的 accessToken
  const characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let accessToken = "";
  for (let i = 0; i < 32; i++) {
    accessToken += characters.charAt(
      Math.floor(Math.random() * characters.length)
    );
  }
  return accessToken;
}

export const config: MockMethod[] = [
  {
    url: "/api/login",
    method: "post",
    response: ({ body }) => {
      if (!body || body.name === '' || body.password === '') {
        return {
          code: 200,
          success: false,
          errMsg: "账号或密码不正确",
          data: {},
        };
      }
      return {
        code: 200,
        success: true,
        errMsg: "",
        data: {
          name: body.name,
          accessToken: generateAccessToken(),
        },
      };
    },
  },
  {
    url: "/api/logout",
    method: "post",
    response: () => {
      return {
        code: 200,
        success: true,
        errMsg: "",
        data: {},
      };
    },
  },
];

export default config;
